<?php

#include
include "../site_specific/project.php";
include "../includes/constants.php";
include "../site_specific/external_constants.php";
include "../includes/phpmailer/class.phpmailer.php";
include "../includes/phpmailer/class.smtp.php";
include "../includes/utilities.php";
include "../includes/pages.php";
include "../includes/database.php";
include "../includes/users.php";
include "../includes/date.php";
include "../includes/text.php";
include "../includes/files.php";
include "../includes/tableproperties.php";
include "../includes/shopping_cart.php";
//include "../includes/init.php";

# Start session
session_start();

# Establish database connection
establishDatabaseConnection();

$selected_parent = $_GET["parent"];

# Create Pages object and provide with basic details of all pages
$PAGES = getSectionPages('site');

# Initialise User
$USER = new User();

$selected_mode=$_GET["flag"];

#if selected value is empty string
if($selected_parent!="clear"){

	#Get the page data according to the selected user id. 
	//$sql="SELECT * FROM mrt_pages WHERE p_parent=\"".$selected_parent."\" AND p_status<3 ORDER by p_sort";
	
	//$result=getRecord($sql);
	
	$output="";
	
	#Loading the module list
	if($selected_mode=='course'){
	
		$output.="<div id=\"div_module\">";
	
		$output.="<select id=\"select_module\" name=\"select_module\" CLASS=\"dropdownlist\" onchange=\"select_page_changed(this.value,'module',false)\">";
		
		$output.="<option value=\"clear\">select...</option>";
		
		$Course_level = $PAGES -> getChildrenOfPage($selected_parent);
		
		# For each
		foreach ($Course_level -> getPageIDs() as $nav_page_id) {
			
			# Get page details
			$nav_page = $Course_level-> getPage($nav_page_id);

			# Get list of groups authorized to view this page
			$nav_page_authorized = explode(",", $nav_page -> getData("p_access"));
			
			# Get List of groups of users
			$nav_group_authorized= explode(",", $USER -> getGroup());
			
			//echo $nav_page_id;
			
			//print_r($PAGES -> page_array);
			

			# Ensure it can be viewed at current authorization
			//$nav_page_authorized = (findArrayInArray($nav_page_authorized, $nav_group_authorized) !== false);
			$nav_page_authorized = $USER -> isUserAuthorized($PAGES -> getAuthorizedGroups($nav_page_id));

			# Ensure it's in the navigation
			$nav_page_in_navigation = ($nav_page -> getData("p_innavigation") == "1");

			# Get class for <li> item
			$item_class = ($nav_page -> getID() == $PAGES -> getPageAtDepth($PAGE_ID, 0)) ? array("active") : array();

			# Is first?
			if ($counter == 0) $item_class[] = "first";

			if ($nav_page_authorized && $nav_page_in_navigation) {
				
				$output.="<option value=\"".$nav_page -> getData("p_id")."\">".$nav_page -> getData("p_name")."</option>";

				$counter++;
			}
		}
	
		$output.="</select>";
	
		$output.="</div>";
		
		$output.="+++---";
		
		$output.="<div id=\"div_page\">";
	
		$output.="<select id=\"select_page\" name=\"select_page\" CLASS=\"dropdownlist\" onchange=\"select_page_changed(this.value,'page',false)\">";
	
		$output.="</select>";
	
		$output.="</div>";
	}
	
	#Loading the page list
	else if($selected_mode=='module'){
		
		$page_button_list="";
		
		$output.="<div id=\"div_page\">";
	
		$output.="<select id=\"select_page\" name=\"select_page\" CLASS=\"dropdownlist\" onchange=\"select_page_changed(this.value,'page',false)\">";
		
		$output.="<option value=\"clear\">select...</option>";
		
		$amount = 0;
		
		$module_level = $PAGES -> getChildrenOfPage($selected_parent);
		
		# For each
		foreach ($module_level -> getPageIDs() as $nav_page_id) {
			
			# Get page details
			$nav_page = $module_level -> getPage($nav_page_id);

			# Get list of groups authorized to view this page
			$nav_page_authorized = explode(",", $nav_page -> getData("p_access"));
			
			# Get List of groups of users
			$nav_group_authorized= explode(",", $USER -> getGroup());

			# Ensure it can be viewed at current authorization
			//$nav_page_authorized = (findArrayInArray($nav_page_authorized, $nav_group_authorized) !== false);
			$nav_page_authorized = $USER -> isUserAuthorized($PAGES -> getAuthorizedGroups($nav_page_id));

			# Ensure it's in the navigation
			$nav_page_in_navigation = ($nav_page -> getData("p_innavigation") == "1");

			# Get class for <li> item
			$item_class = ($nav_page -> getID() == $PAGES -> getPageAtDepth($PAGE_ID, 0)) ? array("active") : array();

			# Is first?
			if ($counter == 0) $item_class[] = "first";

			if ($nav_page_authorized && $nav_page_in_navigation) {
				$amount ++;
			}
		}
		
		$count=1;
		
		$module_level = $PAGES -> getChildrenOfPage($selected_parent);
		
		# For each
		foreach ($module_level -> getPageIDs() as $nav_page_id) {
			
			# Get page details
			$nav_page = $module_level -> getPage($nav_page_id);

			# Get list of groups authorized to view this page
			$nav_page_authorized = explode(",", $nav_page -> getData("p_access"));
			
			# Get List of groups of users
			$nav_group_authorized= explode(",", $USER -> getGroup());
			
			//$PAGES -> getAuthorizedGroups($nav_page_id);

			# Ensure it can be viewed at current authorization
			//$nav_page_authorized = (findArrayInArray($nav_page_authorized, $nav_group_authorized) !== false);
			$nav_page_authorized = $USER -> isUserAuthorized($PAGES -> getAuthorizedGroups($nav_page_id));

			# Ensure it's in the navigation
			$nav_page_in_navigation = ($nav_page -> getData("p_innavigation") == "1");

			# Get class for <li> item
			$item_class = ($nav_page -> getID() == $PAGES -> getPageAtDepth($PAGE_ID, 0)) ? array("active") : array();

			# Is first?
			if ($counter == 0) $item_class[] = "first";

			if ($nav_page_authorized && $nav_page_in_navigation) {			

				$output.="<option value=\"".$nav_page -> getData("p_id")."\">".$nav_page -> getData("p_name")."</option>";
				
				$page_button_list.="<a href=\"javascript:select_page_changed('".$nav_page -> getData("p_id")."','page',true);\" title=\"".$nav_page -> getData("p_name")."\" onmousemove=\"DisplayPageName('".$nav_page -> getData("p_name")."')\" onclick=\"ClickImageButton('".$count."',".$amount.");\"><img width=\"18px\" id=\"img".$count."\" src=\"../site_specific/assets/images/buttons/grey".$count.".png\" border=\"0\"/></a>&nbsp;";
				
				$count++;
				
				$counter++;
			}
		}
		
		$page_button_list ="<a id='a_previous' class='prenext' href=\"javascript:select_page_changed('','page',false);\">PREVIOUS</a>&nbsp;".$page_button_list;
		
		$page_button_list .= "<a id='a_next' class='prenext' href=\"javascript:select_page_changed('','page',false);\">NEXT</a>&nbsp;";
	
		$output.="</select>";
	
		$output.="</div>";
		
		$output.="+++---";
		
		$output.=$page_button_list;
	}
	
	$sql="SELECT * FROM ".$PROJECT_DB_TABLES['pages']." WHERE p_id=\"".$selected_parent."\"";
	
	$result=getRecord($sql);
	
	#########################################################################
	# GET Previous page id and next page id
	########################################################################
	
	$page_level_parent = $PAGES -> getChildrenOfPage($result[0]["p_parent"]);
	
	$index = 0;
	
	$index_current_item = 0;
	
	#Page list for preious next
	$pagelist = array();
	
	# For each
	foreach ($page_level_parent -> getPageIDs() as $nav_page_id) {
			
		  # Get page details
		  $nav_page = $page_level_parent -> getPage($nav_page_id);

		  # Get list of groups authorized to view this page
		  $nav_page_authorized = explode(",", $nav_page -> getData("p_access"));
		  
		  # Get List of groups of users
		  $nav_group_authorized= explode(",", $USER -> getGroup());
		  
		  //$PAGES -> getAuthorizedGroups($nav_page_id);

		  # Ensure it can be viewed at current authorization
		  //$nav_page_authorized = (findArrayInArray($nav_page_authorized, $nav_group_authorized) !== false);
		  $nav_page_authorized = $USER -> isUserAuthorized($PAGES -> getAuthorizedGroups($nav_page_id));

		  # Ensure it's in the navigation
		  $nav_page_in_navigation = ($nav_page -> getData("p_innavigation") == "1");

		  # Get class for <li> item
		  $item_class = ($nav_page -> getID() == $PAGES -> getPageAtDepth($PAGE_ID, 0)) ? array("active") : array();

		  # Is first?
		  if ($counter == 0) $item_class[] = "first";

		  if ($nav_page_authorized && $nav_page_in_navigation) {
			  
			  array_push($pagelist,$nav_page -> getData("p_id"));	
				
			  if($nav_page -> getData("p_id") == $selected_parent){
				  
				  $index_current_item =  $index;
			  }
			  $index++;
		  }
	}
	$pageids = $pagelist;
	
	$previous_id = "";
	
	$next_id = "";
	
	if($index_current_item>=1){
	
		$previous_id = $pageids[$index_current_item-1];
	}
	else{
		
		$previous_id ="";
	}
	if($index_current_item <= count($pageids)-1){
		
		$next_id = $pageids[$index_current_item+1];
	}
	else{
		
		$next_id = "";
	}
	
	$prenext_content = $previous_id."+++---".$next_id;
	
	############################################################################
	#Page content
	#############################################################################
	
	$page_content=$result[0]["p_content"];
	
	$main_output = parseFlashLinks(parseFlashXML($page_content));
	
	#Change the original hyper link style to the one with css
	$main_output = ChangeHyperLinkStyle($main_output);
							  
	$multiple_choice_searching_item_begin="<multiple-choice>";
							  
	$multiple_choice_searching_item_end="</multiple-choice>";
							  
	if(strpos($main_output,$multiple_choice_searching_item_begin)&& strpos($main_output,$multiple_choice_searching_item_begin)){
								  
		$main_output=ReplaceMulitpleChoice($main_output,false);
	}
					
	$seaching_content = getRecord("SELECT gl_letter,gl_term,gl_descripto FROM mrt_glossary order by gl_letter");
							  
	$question_searching_item="<a href=\"www.uq.edu.au\">solution</a>";
	
	$main_output=ReplaceFontFamily($main_output);		
							  
	$main_output=marked_keyword_replace($main_output,$seaching_content);			  
							  
	$main_output=glossary_replace($main_output,$seaching_content);
							  
	#remove the none link Indentifier
	$main_output=RemoveHyperlink($main_output,"***","###");
							  
	#replace the reference indetifier.
	$main_output=Replace_reference($main_output);
							  
																		  
	if(strpos($main_output,$question_searching_item)){
		  $main_output=removetable($main_output);
		  $exersice_array=explode($question_searching_item,$main_output);
		  $main_output=$exersice_array[0];
		  $main_output.="<a href=\"javascript:expandAnswer('".$selected_parent."',1,'div_quesiton_answer_0','answer_expand_img_0');\"><img id=\"answer_expand_img_0\" border=\"0\" src=\"../assets/images/tree/folder_closed_1.png\"></a><div id=\"div_quesiton_answer_0\"></div>";
	}
	
	//$html_symbol=array('&#914;');
//
//	$css_symbol=array("&#66;");
//	
//	$main_output = str_replace($html_symbol, $css_symbol, $main_output);
	
	$main_output=str_replace($question_searching_item,"",$main_output);
	
	if($selected_mode=='course' || $selected_mode=='module'){
		echo $output."+++---".$main_output;
	}
	else{
		
		echo $previous_id."+++---".$next_id."+++---".$main_output;
	}
}
else{
	
	$output="";
	
	$main_output="";
	
	#If the passed id is course id
	if($selected_mode=="course"){
		
		$output.="<div id=\"div_module\">";
	
		$output.="<select id=\"select_module\" name=\"select_module\" CLASS=\"dropdownlist\" onchange=\"select_page_changed(this.value,'module',false)\">";
	
		$output.="</select>";
	
		$output.="</div>";
		
		#Put the instruction in the main content
		$sql_instruction = "SELECT * FROM ".$PROJECT_DB_TABLES['pages']." WHERE p_id = \"instruction\"";
		
		$result_instruction = getRecord($sql_instruction);
		
		$main_output = parseFlashLinks(parseFlashXML($result_instruction[0]["p_content"]));
		
		$output.="+++---";
		
		$output.="<div id=\"div_page\">";
	
		$output.="<select id=\"select_page\" name=\"select_page\" CLASS=\"dropdownlist\" onchange=\"select_page_changed(this.value,'page',false)\">";
	
		$output.="</select>";
	
		$output.="</div>";
	}
	else if($selected_mode=="module"){
		$output.="<div id=\"div_page\">";
	
		$output.="<select id=\"select_page\" name=\"select_page\" CLASS=\"dropdownlist\" onchange=\"select_page_changed(this.value,'page',false)\">";
	
		$output.="</select>";
	
		$output.="</div>";
		
		$output.="+++---";
		
		$main_output="";
	}
	if($selected_mode=="page"){
		echo "+++---+++---";
	}
	else{
		echo $output."+++---".$main_output;
	}
}
?>